package net.lmc.realtime.traffic.controller;

import com.alibaba.fastjson.JSON;
import net.bwie.realtime.traffic.service.trafficrealtimeservice.bean.SugarOne;
import net.lmc.realtime.traffic.bean.*;
import net.lmc.realtime.traffic.service.TrafficFlowService;
import net.lmc.realtime.traffic.service.TrafficService;
import net.lmc.realtime.traffic.service.TransportationService;
import net.lmc.realtime.traffic.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/traffic")
public class TrafficController {
    @Autowired
    TrafficService trafficService;

    @Autowired
    TrafficFlowService trafficFlowService;

    @Autowired
    private TransportationService transportationService;


    static final String defaultDate = "1970-01-01";

    @RequestMapping("/illegalEvents")
    public String getIllegalEvents(@RequestParam(value = "date",defaultValue = defaultDate)String date) {

        if ((defaultDate.equals(date))) {
            date = DateUtil.nowDate();
        }

        List<SugarIllegalEvents> illegalEventsList = trafficService.getIllegalEvents(date);

        if(null == illegalEventsList || illegalEventsList.isEmpty()){
            return "[]";
        }

        ArrayList<SugarOne> name = new ArrayList<>();
        name.add(new SugarOne("车牌号", "licensePlate"));
        name.add(new SugarOne("道路ID",  "roadId"));
        name.add(new SugarOne("车道号", "lane"));
        name.add(new SugarOne("事件类型",  "eventType"));

        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"hitSugarSelf\": true,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"columns\": "+
                JSON.toJSONString(name)
                +",\n" +
                "    \"rows\": "+
                JSON.toJSONString(illegalEventsList)
                +"\n" +
                "  }\n" +
                "}";
    }

    @RequestMapping("/illegalType")
    public String getIllegalType(@RequestParam(value = "date",defaultValue = defaultDate)String date) {

        if ((defaultDate.equals(date))) {
            date = DateUtil.nowDate();
        }

        List<SugarTwo> illegalTypeList = trafficService.getIllegalType(date);

        if(null == illegalTypeList || illegalTypeList.isEmpty()){
            return "[]";
        }

        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"hitSugarSelf\": true,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": "+
                JSON.toJSONString(illegalTypeList)
                +"\n" +
                "}";
    }


    @RequestMapping("/flow")
    public String getFlowCardCount(@RequestParam(value = "date",defaultValue =defaultDate)String date) {
        if (defaultDate.equals(date)){
            date = DateUtil.nowDate();
        }

        List<TrafficFlowBean> trafficFlowBeanList = trafficFlowService.getFlowCardCount(date);

        List<String> Camera_id = trafficFlowBeanList.stream().map(TrafficFlowBean::getCamera_id).collect(Collectors.toList());
        List<Long> License_plate_count = trafficFlowBeanList.stream().map(TrafficFlowBean::getLicense_plate_count).collect(Collectors.toList());

        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"hitSugarSelf\": true,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"categories\": "+
                    JSON.toJSONString(Camera_id)
                +",\n" +
                "    \"series\": [\n" +
                "      {\n" +
                "        \"name\": \"流量\",\n" +
                "        \"data\": "+
                JSON.toJSONString(License_plate_count)
                +"\n" +
                "      }\n" +
                "    ]\n" +
                "  }\n" +
                "}";
    }


    @RequestMapping("/flowType")
    public String getFlowCardTypeCount(@RequestParam(value = "date",defaultValue =defaultDate)String date) {
        if (defaultDate.equals(date)){
            date = DateUtil.nowDate();
        }

        List<TrafficFlowTypeBean> trafficFlowTypeBeanList = trafficFlowService.getFlowCardTypeCount(date);

        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"hitSugarSelf\": true,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": "+
                JSON.toJSONString(trafficFlowTypeBeanList)
                +"\n" +
                "}";
    }


    @RequestMapping("/cityOverview/totalFlow")
    public String selectTotalFlow(@RequestParam(value = "date",defaultValue = defaultDate)String date)
    {

        if (defaultDate.equals(date)){
            date = DateUtil.nowDate();
        }

        List<TransportationMetrics> cityOverview = transportationService.selectCityOverview(date);

        if (null == cityOverview || cityOverview.isEmpty()){
            return "[]";
        }

        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"hitSugarSelf\": true,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": "+
                cityOverview.get(0).getTotalFlow()
                +"\n" +
                "}";
    }

    @RequestMapping("/cityOverview/congestionRate")
    public String selectCongestionRate(@RequestParam(value = "date",defaultValue = defaultDate)String date)
    {

        if (defaultDate.equals(date)){
            date = DateUtil.nowDate();
        }

        List<TransportationMetrics> cityOverview = transportationService.selectCityOverview(date);

        if (null == cityOverview || cityOverview.isEmpty()){
            return "[]";
        }

        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"hitSugarSelf\": true,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": "+
                cityOverview.get(0).getCongestionRate()
                +"\n" +
                "}";
    }

    @RequestMapping("/cityOverview/avgSpeed")
    public String selectAvgSpeed(@RequestParam(value = "date",defaultValue = defaultDate)String date)
    {

        if (defaultDate.equals(date)){
            date = DateUtil.nowDate();
        }

        List<TransportationMetrics> cityOverview = transportationService.selectCityOverview(date);

        if (null == cityOverview || cityOverview.isEmpty()){
            return "[]";
        }

        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"hitSugarSelf\": true,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {"+
                "    \"max\": 100,\n" +
                "    \"min\": 0,\n" +
                "    \"name\": \"平均车速\",\n" +
                "    \"unit\": \"\",\n" +
                "    \"value\": " + cityOverview.get(0).getAvgSpeed() + ",\n" +
                "    \"hideName\": false"
                +"  }\n" +
                "}";
    }

    @RequestMapping("/top")
    public String func01(){
//        trafficMonitoring
        List<TrafficMonitoring> top = transportationService.getTrafficMonitoring();
//        ArrayList<TrafficMonitoring> list = new ArrayList<>();
        if (top == null || top.isEmpty()){
            return "[]";
        }
//        return JSON.toJSONString(top);

        ArrayList<SugarOne> name = new ArrayList<>();
        name.add(new SugarOne("道路ID",  "roadId"));
        name.add(new SugarOne("平均车速", "avgspeed"));
        name.add(new SugarOne("是否拥挤",  "congestion_level"));

        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"hitSugarSelf\": true,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"columns\": "+
                JSON.toJSONString(name)
                +",\n" +
                "    \"rows\": "+
                JSON.toJSONString(top)
                +"\n" +
                "  }\n" +
                "}";
    }



}
